package com.longcai.cm.api.api4order;

import com.longcai.cm.auth.token.LoginUserInfo;
import com.longcai.cm.auth.token.RequestUtil;
import com.longcai.cm.bean.R;
import com.longcai.cm.domain.FaDschool;
import com.longcai.cm.domain.JaMemberCollection;
import com.longcai.cm.domain.JaOrderComment;
import com.longcai.cm.domain.JaShop;
import com.longcai.cm.service.FaDschoolService;
import com.longcai.cm.service.JaMemberCollectionService;
import com.longcai.cm.service.JaOrderCommentService;
import com.longcai.cm.service.JaShopService;
import com.longcai.cm.utils.GeoCalculator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author libingxing
 */
@RestController
@Api(tags = "门店相关API")
@RequestMapping("/api/shop")
public class ApiShopController {

    @Resource
    private JaShopService shopService;

    @Resource
    private FaDschoolService dschoolService;

    @Resource
    private JaOrderCommentService orderCommentService;

    @Resource
    private JaMemberCollectionService memberCollectionService;

    @PostMapping("/shopInfo")
    @ApiOperation("门店详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", dataType = "String", required = true),
            @ApiImplicitParam(value = "纬度", name = "lat", dataType = "String", required = true),
            @ApiImplicitParam(value = "经度", name = "lng", dataType = "String", required = true),
    })
    public R<JaShop> shopInfo(
            String id, HttpServletRequest request, double lat, double lng
    ) {
        int memberId = 0;
        try {
            final LoginUserInfo userInfo = RequestUtil.getUserInfoByRequest(request);
            memberId = Integer.parseInt(userInfo.getUserId());
        } catch (Exception ignore) {
        }
        final JaShop shop = shopService.getById(id);
        try {
            shop.setDistance(GeoCalculator.calculateDistance(lat, lng, new BigDecimal(shop.getLat()).doubleValue(), new BigDecimal(shop.getLng()).doubleValue()));
        } catch (Exception e) {
            shop.setDistance(0);
        }
        final Long commentCount = orderCommentService.lambdaQuery().eq(JaOrderComment::getShopId, shop.getId())
                .count();
        shop.setCommentCount(commentCount);
        if (memberId > 0) {
            final JaMemberCollection one = memberCollectionService.lambdaQuery().eq(JaMemberCollection::getMemberId, memberId)
                    .eq(JaMemberCollection::getTransId, id).eq(JaMemberCollection::getType, 3).one();
            shop.setIfCollect(one == null ? 0 : 1);
        }else {
            shop.setIfCollect(0);
        }
        return R.success(shop);
    }

    public static void main(String[] args) {
        System.err.println(GeoCalculator.calculateDistance(37.79467, 112.54684,37.111111, 112.666666));
    }

    @PostMapping("/schoolInfo")
    @ApiOperation("驾校详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", dataType = "String", required = true)
    })
    public R<FaDschool> schoolInfo(
            String id
    ) {
        final FaDschool school = dschoolService.getById(id);
        return R.success(school);
    }

}
